from django.db import transaction
from django.db.models import Max
from lotto.catalog.models import *
from lotto.service.parser import ParseLotto
from lotto.const import GAME


def next (cls):
    return int(GAME[cls].objects.all ().aggregate (Max ('id'))['id__max']) + 1;

def parse (mode):
    if mode == 'lotto':
        parse_lotto (next (mode));
    elif mode == 'mini':
        parse_mini (next (mode));
    elif mode == 'multi':
        parse_multi (next (mode));
    elif mode == 'kaskada':
        parse_kaskada (next (mode));
    elif mode == 'joker':
        parse_joker (next (mode));
    else:
        raise ValueError ("Available values:<lotto, mini, multi, kaskada, joker>");


@transaction.commit_manually        
def parse_lotto (next_id):
    reader = ParseLotto ().parse ('lotto', next_id);
    try:
        for r in reader:
            l = Lotto (date = r[1],
                       n1 = r[2][0], n2 = r[2][1], n3 = r[2][2],
                       n4 = r[2][3], n5 = r[2][4], n6 = r[2][5]);
            l.save ();
    except: 
        transaction.rollback ();
        return 'Lotto - nastapil blad podczas aktualizowania danych';
    else:
        transaction.commit ();
        return 'Lotto - dane zostaly poprawnie zaktualizowane';
        

@transaction.commit_manually        
def parse_mini (next_id):
    reader = ParseLotto ().parse ('mini', next_id);
    try:
        for r in reader:
            m = MiniLotto (date = r[1],
                           n1 = r[2][0], n2 = r[2][1], n3 = r[2][2],
                           n4 = r[2][3], n5 = r[2][4]);
            m.save ();
    except: 
        transaction.rollback ();
        return 'MiniLotto - nastapil blad podczas aktualizowania danych';
    else:
        transaction.commit ();
        return 'MiniLotto - dane zostaly poprawnie zaktualizowane';
        

@transaction.commit_manually        
def parse_multi (next_id):
    reader = ParseLotto ().parse ('multi', next_id);
    try:
        for r in reader:
            m = MultiMulti (date = r[1],
                            n1 = r[2][0], n2 = r[2][1], n3 = r[2][2],
                            n4 = r[2][3], n5 = r[2][4], n6 = r[2][5],
                            n7 = r[2][6], n8 = r[2][7], n9 = r[2][8],
                            n10 = r[2][9], n11 = r[2][10], n12 = r[2][11],
                            n13 = r[2][12], n14 = r[2][13], n15 = r[2][14],
                            n16 = r[2][15], n17 = r[2][16], n18 = r[2][17],
                            n19 = r[2][18], pls = r[2][19]);
            m.save ();
    except: 
        transaction.rollback ();
        return 'MultiMulti - nastapil blad podczas aktualizowania danych';
    else:
        transaction.commit ();
        return 'MultiMulti - dane zostaly poprawnie zaktualizowane';


@transaction.commit_manually        
def parse_joker (next_id):
    reader = ParseLotto ().parse ('joker', next_id);
    try:
        for r in reader:
            m = Joker (date = r[1],
                       jr = r[2][4], n1 = r[2][0], n2 = r[2][1],
                       n3 = r[2][2], n4 = r[2][3]);
            m.save ();
    except: 
        transaction.rollback ();
        return 'Joker - nastapil blad podczas aktualizowania danych';
    else:
        transaction.commit ();
        return 'Joker - dane zostaly poprawnie zaktualizowane';


@transaction.commit_manually        
def parse_kaskada (next_id):
    reader = ParseLotto ().parse ('kaskada', next_id);
    try:
        for r in reader:
            m = Kaskada (date = r[1],
                         n1 = r[2][0], n2 = r[2][1], n3 = r[2][2],
                         n4 = r[2][3], n5 = r[2][4], n6 = r[2][5],
                         n7 = r[2][6], n8 = r[2][7], n9 = r[2][8],
                         n10 = r[2][9], n11 = r[2][10], n12 = r[2][11]);
            m.save ();
    except: 
        transaction.rollback ();
        return 'Kaskada - nastapil blad podczas aktualizowania danych';
    else:
        transaction.commit ();
        return 'Kaskada - dane zostaly poprawnie zaktualizowane';


